/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ssm.changeunit;

import com.ssm.changeunit.persistance.BranchDAO;
import com.ssm.changeunit.persistance.BranchDTO;
import com.ssm.changeunit.persistance.PersistanceException;
import org.apache.log4j.Logger;

/**
 *
 * @author 시리우스
 */
public class BranchFactory extends AbstractFactory {
    private Logger log = Logger.getLogger(BranchFactory.class);
    
    @Override
    public Branch createProduct(String branchName) throws CreateException {
        try {
            BranchDAO branchDAO = new BranchDAO();
            
            BranchDTO branchDTO = branchDAO.find(branchName);
            
            if(branchDTO == null) {
                log.debug("기존 Branch를 찾을 수 없어 새로운 Branch를 추가합니다.");
                
                branchDTO = new BranchDTO();
                
                branchDTO.setBranchName(branchName);
                branchDTO.setChangeUnitId("Unknown");
                branchDTO.setOriginChangeUnitId("Unknown");
                
                branchDAO.insert(branchDTO);
            }
            
            return new Branch(branchName);
        }
        catch(PersistanceException ex) {
            throw new CreateException("Branch를 생성하는데 실패했습니다.", ex);
        }
    }
}
